/**
 *  Class OtrDecoderProcess is part of the project OtrMediaManager
    Copyright (C) 2014  Gerrit Eichel

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package otrmediamanager.otr;

import java.io.*;
import java.util.Arrays;
import java.util.List;

import otrmediamanager.utils.*;

/**
 * This class implements a OTR Decoder (command line version) wrapper.
 */
public class OtrDecoderProcess {
	/**	The OTR decoder full path to be called from command line. */
	private static String OTR_DECODER_BINARY_FULLPATH = "./otrdecoder";
	
	/** The OTR decoder options. */
	private OtrDecoderOptions _otrDecoderOptions = null;
	
	/**
	 * Initializes a new OtrDecoderProcess instance.
	 * @param otrDecoderOptions OTR decoder options to be used.
	 * @throws FileNotFoundException The exception is thrown if the OTR source file or OTR output folder is not found.
	 */
	public OtrDecoderProcess(OtrDecoderOptions otrDecoderOptions) throws FileNotFoundException {
		this.setOtrDecoderOptions(otrDecoderOptions);
	}
	
	/**
	 * Returns the OTR decoder options.
	 * @return The OTR decoder options.
	 */
	public OtrDecoderOptions getOtrDecoderOptions() {
		return this._otrDecoderOptions;
	}
	/**
	 * Sets OTR decoder options.
	 * @param otrDecoderOptions The OTR decoder options to be set.
	 */
	private void setOtrDecoderOptions(OtrDecoderOptions otrDecoderOptions) {
		if (otrDecoderOptions == null) {
			throw new java.lang.IllegalArgumentException("Property <OtrDecoderOptions> is not initialized!");
		}
		this._otrDecoderOptions = otrDecoderOptions;
	}
	/**
	 * Executes OTR decoder process.
	 * @throws IOException
	 */
	public void ExecuteOtrDecoder() throws IOException {
		List<String> commandLine = Arrays.asList("/bin/sh", "-c", 
				OTR_DECODER_BINARY_FULLPATH + this.getOtrDecoderOptions().getOtrDecoderCommandLineOptions());
		ConsoleApplicationProcessWrapper otrDecoderProcess = new ConsoleApplicationProcessWrapper(commandLine);
		try {
			String lastLine = "";
			otrDecoderProcess.start();
			while (otrDecoderProcess.getState() != Thread.State.TERMINATED) {
				String currentLine = otrDecoderProcess.getLastConsoleLine(); 
				if (currentLine != lastLine) {
					lastLine = currentLine;
					System.out.println(currentLine);
				}
            	Thread.sleep(1);
            }            
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}